//
//  LKComposeTextView.swift
//  LKWeibo
//
//  Created by like on 16/8/17.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

class LKComposeTextView: UITextView {
    
    //显示的占位文字的大小
    override var font: UIFont?{
        didSet{
            placeholderLabel.font = font
        }
    }
    
    // 显示的占位文字
    var placeholder: String?{
        didSet{
            // 设置占位文字的text
            placeholderLabel.text = placeholder
        }
    }
    

    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(placeholderLabel)
        
        //设置约束
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(10)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(SCREENW - 10)
        }
        
        // 监听系统通知 查看textView 文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(LKComposeTextView.textViewTextDidChange), name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    //监听 方法
    @objc private func textViewTextDidChange(){
        
        placeholderLabel.hidden = self.hasText()
    }
    
    
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    //懒加载label  站位图片
    
    private lazy var placeholderLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.redColor()
//        lab.font = UIFont.systemFontOfSize(15)
//        lab.text = "佛挡杀佛佛挡杀佛佛挡杀佛佛挡杀佛佛挡杀佛"
        lab.numberOfLines = 0
        return lab
    }()
    
    
}
